package yunhe.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class AccessFilter implements Filter {

	
	public void destroy() {
	
		
	}

	
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req=(HttpServletRequest) request;
		HttpServletResponse resp=(HttpServletResponse) response;
		
		String path=req.getServletPath();
		if("/admin/index.jsp".equals(path)||!path.contains(".")){//servlet也放行
			chain.doFilter(request, response);
		}
		else{
			HttpSession session=req.getSession();
			Object obj=session.getAttribute("loginState");
			if(obj==null||obj.equals(false)){
				resp.sendRedirect(req.getContextPath()+"/admin/index.jsp");//未登陆，返回登陆页面
			}
			else{
				chain.doFilter(request, response);
			}
		}
	}


	public void init(FilterConfig arg0) throws ServletException {
		
		
	}

}
